/**
 *
 */
package cn.hg.jfsa.manage.controller.module.ad;

import java.sql.SQLException;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;

import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.base.kit.StrExtKit;
import cn.hg.jfsa.core.JfsaThreadLocal;
import cn.hg.jfsa.manage.controller.module.ad.validator.AdValidator;
import cn.hg.jfsa.manage.service.AdService;
import cn.hg.jfsa.manage.web.LayuiPage;
import cn.hg.jfsa.model.Ad;
import cn.hg.jfsa.model.JfsaAdmin;
import cn.hg.jfsa.web.theme.ThemeManager;

/**
 * @author golly
 */
public class AdController extends BaseController {

	/**
	 * 广告位置服务
	 */
	@Inject
	public static AdService adService;

	/**
	 * 跳转到新增页面
	 */
	public void add () {
		this.setAttr("adPlaceList", AdController.adService.findPlaceByStatus("on"));
		// 创建一个新的记录并传给前端页面
		this.setAttr("ad", new Ad().setSortNum(0).setType(""));
	}

	/**
	 * Ajax删除功能
	 */
	@Before (EvictInterceptor.class)
	@CacheName ("ad")
	public void ajaxDel () {
		// 通过页面传入的ID查找到记录
		Ad bean = Ad.DAO.findById(this.getParaToInt("id"));

		// 记录不存在则报错
		if (null == bean) {
			this.renderAjaxFailed("1", "记录不存在！");
			return;
		}

		// 更新记录的状态为删除
		bean.setStatus("del").update();
		// 返回Ajax成功
		this.renderAjaxSuccessed();
	}

	/**
	 * Ajax方式新建或更新记录
	 */
	@Before ({
		AdValidator.class, EvictInterceptor.class
	})
	@CacheName ("ad")
	public void ajaxSaveOrUpdate () {
		// 获取页面传入的Bean
		Ad bean = this.getBean(Ad.class, "ad");
		JfsaAdmin admin = JfsaThreadLocal.getAdmin();
		// 这里引用事务处理是为了方便Ajax模式处理
		boolean successed = Db.tx(new IAtom() {

			@Override
			public boolean run () throws SQLException {

				if (null == bean.getId()) {
					// 新建记录
					bean.setTheme(ThemeManager.me().getCurrentTheme().getId())
						.setCreatorId(admin.getId())
						.setCreatedTime(new Date())
						.setEditorId(admin.getId())
						.setEditedTime(new Date())
						.save();
				} else {
					// 更新记录
					bean.setEditorId(admin.getId())
						.setEditedTime(new Date())
						.update();
				}

				return true;
			}

		});

		// 返回Ajax数据
		if (successed) {
			this.renderAjaxSuccessed();
		} else {
			this.renderAjaxFailed("-1", "服务器错误，请重试。");
		}

	}

	/**
	 * Ajax方式查询
	 */
	@Before (CacheInterceptor.class)
	@CacheName ("ad")
	public void ajaxSearch () {
		// 建立查询键值对
		Kv para = Kv.by("title", this.getPara("title"))
			.set("placeId", this.getPara("placeId"))
			.set("device", this.getPara("device"))
			.set("type", this.getPara("type"))
			.set("status", this.getPara("status"))
			.set("sortField", StrExtKit.humpToUnderline(this.getPara("sortField")))
			.set("sortType", this.getPara("sortType"))
			.set("theme", ThemeManager.me().getCurrentTheme().getId());
		// 通过SQL模板引擎构建SQL及参数
		SqlPara sqlPara = Db.getSqlPara("module.findAd", para);
		// 执行查询
		Page<Ad> page = Ad.DAO.paginate(this.getParaToInt("page"), this.getParaToInt("limit"), sqlPara);
		// 返回Ajax调用的Json结果
		this.renderJson(new LayuiPage(page));
	}

	/**
	 * 跳转到编辑页面
	 */
	public void edit () {
		this.setAttr("adPlaceList", AdController.adService.findPlaceByStatus("on"));
		// 通过ID取得记录并传给前端页面
		this.setAttr("ad", Ad.DAO.findById(this.getParaToInt("id")));
	}

	/**
	 * 跳转到列表页面
	 */
	@Override
	@Before (CacheInterceptor.class)
	@CacheName ("ad")
	public void index () {
		this.setAttr("adPlaceList", AdController.adService.findPlaceByStatus("on"));
	}

	/**
	 * 跳转到查看页面
	 */
	@Before (CacheInterceptor.class)
	@CacheName ("ad")
	public void view () {
		this.setAttr("adPlaceList", AdController.adService.findPlaceByStatus("on"));
		this.setAttr("ad", Ad.DAO.findById(this.getParaToInt("id")));
	}

}
